﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using OL.FrameCore.Application;
using Microsoft.AspNetCore.Authorization;
using OL.FrameCore.WebUI.Code;
using OL.FrameCore.Infrastructure.Utility;
using OL.FrameCore.Infrastructure.Security;
using Microsoft.AspNetCore.Http;

namespace OL.FrameCore.WebUI.Controllers
{
    /// <summary>
    /// 基类控制器，提供获取用户id、用户姓名、返回封装json结果等
    /// </summary>
    [Authorize]
    [ServiceFilter(typeof(VisitFilterAttribute))]//构造函数带参数的方式，Startup中需注册services.AddScoped<VisitFilterAttribute>()
    public class ControllerBase : Controller
    {
        //private readonly IHttpContextAccessor _httpContextAccessor;
        //public ControllerBase()
        //{
            
        //}


        public int UserId
        {
            get
            {
                string user = User.Identity.Name;
                var claim = User.Claims.Where(c => c.Type == ClaimTypes.Sid).Single();
                return int.Parse(claim.Value);
            }
        }

        public string UserName
        {
            get
            {
                string user = User.Identity.Name;
                return user;
            }
        }

        //public long TrueName
        //{
        //    get
        //    {
        //        //string user = User.Identity.Name;
        //        var claim = User.Claims.Where(c => c.Type == "TrueName").Single();
        //        return long.Parse(claim.Value);
        //    }
        //}

        public JsonResult SuccessResult(string msg = "")
        {
            return SuccessResult<DBNull>(null);
        }
        public JsonResult SuccessResult<T>(T t, string msg = "")
        {
            Result<T> result = new Result<T>
            {
                State = 1,
                Msg = msg,
                Data = t
            };
            return Json(result);
        }
        public JsonResult FailResult(string msg = "")
        {
            return FailResult<DBNull>(DBNull.Value, msg);
        }
        public JsonResult FailResult<T>(T t, string msg = "")
        {
            Result<T> result = new Result<T>
            {
                State = 0,
                Msg = msg,
                Data = t
            };
            return Json(result);
        }

        public JsonResult ErrorResult(string msg)
        {
            Result<DBNull> result = new Result<DBNull>
            {
                State = -1,
                Msg = msg
            };
            return Json(result);
        }

        /// <summary>
        /// 是否模型未验证通过
        /// </summary>
        public bool IsNotValidModel
        {
            get {
                return !ModelState.IsValid;
            }
        }
        /// <summary>
        /// 模型异常信息
        /// </summary>
        public string ModelErrorMessage
        {
            get
            {
                if (!ModelState.IsValid)
                {
                    foreach (var key in ModelState.Keys)
                    {
                        var modelstate = ModelState[key];
                        if (modelstate.Errors.Any())
                        {
                            string error = modelstate.Errors.FirstOrDefault().ErrorMessage;
                            return error;
                        }
                    }
                    return "";
                }
                else
                {
                    return "";
                }
            }
        }

        /// <summary>
        /// 对象映射
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public T2 ToMap<T1,T2>(T1 model)
        {
            return AutoMapperHelper.ToMap<T1, T2>(model);
        }
        /// <summary>
        /// 对象映射
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public IEnumerable<T2> ToMap<T1, T2>(IEnumerable<T1> list)
        {
            return AutoMapperHelper.ToMap<T1, T2>(list);
        }

        /// <summary>
        /// RSA解密
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        protected string DecryptRSA(string password)
        {
            return RSAHelper.DecryptRSA(password);
        }

        ///// <summary>
        ///// 获取ip
        ///// </summary>
        ///// <returns></returns>
        //protected string GetIP()
        //{
        //    return this._httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
        //}
    }
}
